package cc.rengu.redp.bizimpl.upmp.controller;
import cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMcListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryMchntFeeDiscountMcListInfoRequest;
import cc.rengu.redp.bizimpl.upmp.bean.CommissionPublishRequest;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserInfo;
import cc.rengu.redp.bizimpl.upmp.service.CommissionMarketService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "营销管理:银行对商户手续费营销活动发布")
@Controller
@RequestMapping(value = "/admin/market/commissionRelease")
public class CommissionMarketReleaseController {

    public static Logger log = LoggerFactory.getLogger(CommissionMarketReleaseController.class);

    @Autowired
    private CommissionMarketService marketService;
    @Autowired
    private IUpmpUserInfoService userService;


    //银行对商户手续费优惠发布查询营销活动
    @GetMapping("/list")
    @ResponseBody
    public IPage<MchntFeeDiscountMcListInfoBean> findByPage(QueryMchntFeeDiscountMcListInfoRequest req,QueryRequest pageable) throws RedpException {
        req.setQueryType("0");
        req.setQueryRole("02");
        IPage<MchntFeeDiscountMcListInfoBean> page=marketService.findByPage(pageable, req);
        if(page!=null && page.getSize()>0){
            List<MchntFeeDiscountMcListInfoBean> list=page.getRecords();
            for(MchntFeeDiscountMcListInfoBean bean:list){
                String userId=bean.getOprId();
                UpmpUserInfo user = userService.findById(userId);
                if(Common.isNotBlank(user)){
                    bean.setOprId(user.getUserName());
                }
            }
        }
        return page;
    }

    //发布
//    @RequiresPermissions("银行对商户手续费优惠发布:发布")
    @PostMapping("/release")
    @ResponseBody
    public OperationResult releaseSave(CommissionPublishRequest entity) {
        if ("1".equals(entity.getFlag())) {
            //TODO
            //需要付款账号
            //付款账户非空校验
        }
        String msg = "发布";
        if ("1".equals(entity.getOprType())) {
            msg = "退回修改";
        }
        entity.setQueryType("0");
        entity.setQueryRole("02");
        try {
            marketService.publishMchntFeeDiscountMc(entity, msg);
        } catch (Exception e) {
            return OperationResult.buildFailureResult(e.getMessage());
        }
        return OperationResult.buildSuccessResult("发布员" + msg + "成功");
    }

 /*   //终止
    @RequiresPermissions("银行对商户手续费优惠发布:终止")
    @RequestMapping(value = "/shutdown", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult shutdownSave(Model model, CommissionPublishRequest entity) {
        String msg = "终止";
        entity.setQueryType("0");
        entity.setQueryRole("02");
        try {
            marketService.shutDownMchntFeeDiscountMc(model, entity, msg);
        } catch (Exception e) {
            return OperationResult.buildFailureResult(e.getMessage());
        }
        return OperationResult.buildSuccessResult("活动" + msg + "成功");
    }
    //暂停
    @RequiresPermissions("银行对商户手续费优惠发布:暂停")
    @RequestMapping(value = "/pause", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult pauseSave(Model model, CommissionPublishRequest entity) {
        String msg = "暂停";
        entity.setQueryType("0");
        entity.setQueryRole("02");
        try {
            marketService.shutDownMchntFeeDiscountMc(model, entity, msg);
        } catch (Exception e) {
            return OperationResult.buildFailureResult(e.getMessage());
        }
        return OperationResult.buildSuccessResult("活动" + msg + "成功");
    }
    //启用
    @RequiresPermissions("银行对商户手续费优惠发布:启用")
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult resumeSave(Model model, CommissionPublishRequest entity) {
        String msg = "启用";
        entity.setQueryType("0");
        entity.setQueryRole("02");
        try {
            marketService.shutDownMchntFeeDiscountMc(model, entity, msg);
        } catch (Exception e) {
            return OperationResult.buildFailureResult(e.getMessage());
        }
        return OperationResult.buildSuccessResult("活动" + msg + "成功");
    }*/
}
